home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / exec / initstruct.c < prev    next >
C/C++ Source or Header  |  1996-09-13  |  5KB  |  259 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: initstruct.c,v 1.5 1996/09/13 17:51:23 digulla Exp $
  4.     $Log: initstruct.c,v $
  5.     Revision 1.5  1996/09/13 17:51:23  digulla
  6.     Use IPTR
  7.  
  8.     Revision 1.4  1996/08/13 13:56:03  digulla
  9.     Replaced __AROS_LA by __AROS_LHA
  10.     Replaced some __AROS_LH*I by __AROS_LH*
  11.     Sorted and added includes
  12.  
  13.     Revision 1.3  1996/08/01 17:41:13  digulla
  14.     Added standard header for all files
  15.  
  16.     Desc:
  17.     Lang: english
  18. */
  19. #include "exec_intern.h"
  20. #include <aros/libcall.h>
  21. #include <exec/alerts.h>
  22. #include "machine.h"
  23.  
  24. /*****************************************************************************
  25.  
  26.     NAME */
  27.     #include <clib/exec_protos.h>
  28.  
  29.     __AROS_LH3(void, InitStruct,
  30.  
  31. /*  SYNOPSIS */
  32.     __AROS_LHA(APTR,  initTable, A1),
  33.     __AROS_LHA(APTR,  memory,    A2),
  34.     __AROS_LHA(ULONG, size,      D0),
  35.  
  36. /*  LOCATION */
  37.     struct ExecBase *, SysBase, 13, Exec)
  38.  
  39. /*  FUNCTION
  40.     Initialize some library base or other structure depending on the
  41.     information in the init table. The init table consists of
  42.     instructions starting with an action byte followed by more
  43.     information. The instruction byte looks like:
  44.  
  45.     iisscccc where ii is the instruction code:
  46.               0 - copy following c+1 elements
  47.               1 - repeat following element c+1 times
  48.               2 - take next byte as offset, then copy
  49.               3 - take the next 3 bytes (in the machine's
  50.                   particular byte ordering) as offset, then
  51.                   copy
  52.                ss is the element size
  53.               0 - LONGs
  54.               1 - WORDs
  55.               2 - BYTEs
  56.                cccc is the element count-1
  57.  
  58.     Instruction bytes must follow the same alignement restrictions as LONGs,
  59.     the following elements are aligned to their particular restrictions.
  60.  
  61.     A 0 instruction ends the init table.
  62.  
  63.     INPUTS
  64.     initTable - Pointer to init table.
  65.     memory      - Pointer to uninitialized structure.
  66.     size      - Size of memory area to 0 out before decoding or 0
  67.             for no filling.
  68.  
  69.     RESULT
  70.  
  71.     NOTES
  72.  
  73.     EXAMPLE
  74.  
  75.     BUGS
  76.  
  77.     SEE ALSO
  78.  
  79.     INTERNALS
  80.  
  81.     HISTORY
  82.  
  83. ******************************************************************************/
  84. {
  85.     __AROS_FUNC_INIT
  86.  
  87.     LONG  cnt;
  88.     ULONG offset=0,src;
  89.     UBYTE *it,*dst;
  90.     int   s,t;
  91.  
  92.     /* Clear Memory area fast. Get number of longs and clear them. */
  93.     cnt=size/sizeof(LONG);
  94.     size&=(sizeof(LONG)-1);
  95.     dst=(UBYTE *)memory;
  96.     if(cnt)
  97.     do
  98.     {
  99.         *(LONG *)dst=0;
  100.         dst+=sizeof(LONG);
  101.     }
  102.     while(--cnt);
  103.  
  104.     /* Clear the rest. */
  105.     cnt=size;
  106.     if(cnt)
  107.     do
  108.         *dst++=0;
  109.     while(--cnt);
  110.  
  111.     it =(UBYTE *)initTable;
  112.     dst=(UBYTE *)memory;
  113.  
  114.     /* As long as there's something to do */
  115.     while(*it!=0)
  116.     {
  117.     /* What to do. */
  118.     t=*it>>6&3;
  119.  
  120.     /* Element size. */
  121.     s=*it>>4&3;
  122.  
  123.     /* Number of things to do (-1). */
  124.     cnt=*it&15;
  125.  
  126.     /* Depending on the action there may be more information */
  127.     switch(t)
  128.     {
  129.         case 0:
  130.         case 1:
  131.         /* Skip the action byte */
  132.         it++;
  133.         break;
  134.         case 2:
  135.         /* Skip the action byte, get the offset */
  136.         it++;
  137.         offset=*it++;
  138.         break;
  139.         case 3:
  140.         /*
  141.             Get 24bit offset. It's the programmer's responsibility
  142.             to align the action byte with a LONG instruction before
  143.             this.
  144.         */
  145. #if BIG_ENDIAN
  146.         offset=*(ULONG *)it&0xffffff;
  147. #else
  148.         offset=*(ULONG *)it>>8;
  149. #endif
  150.         it+=sizeof(LONG);
  151.         break;
  152.     }
  153.  
  154.     /* Align source and destination pointers */
  155.     switch(s)
  156.     {
  157.         case 0:
  158.         /* Align pointer to LONG requirements */
  159.         it =(UBYTE *)(((IPTR)it +LONGALIGN-1)&~(LONGALIGN-1));
  160.         dst=(UBYTE *)(((IPTR)dst+LONGALIGN-1)&~(LONGALIGN-1));
  161.         break;
  162.         case 1:
  163.         /* Same for WORDs */
  164.         it =(UBYTE *)(((IPTR)it +WORDALIGN-1)&~(WORDALIGN-1));
  165.         dst=(UBYTE *)(((IPTR)dst+WORDALIGN-1)&~(WORDALIGN-1));
  166.         break;
  167.         case 2:
  168.         /* Nothing to do for bytes */
  169.         break;
  170.         default:
  171.         /* And an Alert for nibbles ;-) */
  172.         Alert(ACPU_AddressErr);
  173.  
  174.         /*
  175.             Tell the compiler that he doesn't need to
  176.             care about side effects of Alert()
  177.         */
  178.         return;
  179.     }
  180.  
  181.     /* Switch over action */
  182.     switch(t)
  183.     {
  184.         case 2:
  185.         case 3:
  186.         /* Action is: Add offset then copy */
  187.         dst=(BYTE *)memory+offset;
  188.  
  189.         /* Fall through */
  190.         case 0:
  191.         /* Action is: Copy the next <cnt> elements to the current location */
  192.         switch(s)
  193.         {
  194.             case 0:
  195.             /* Copy loop */
  196.             do
  197.             {
  198.                 *(LONG *)dst=*(LONG *)it;
  199.                 dst+=sizeof(LONG);
  200.                 it +=sizeof(LONG);
  201.             }while(--cnt>=0);
  202.             break;
  203.             case 1:
  204.             do
  205.             {
  206.                 *(WORD *)dst=*(WORD *)it;
  207.                 dst+=sizeof(WORD);
  208.                 it +=sizeof(WORD);
  209.             }while(--cnt>=0);
  210.             break;
  211.             case 2:
  212.             do
  213.                 *dst++=*it++;
  214.             while(--cnt>=0);
  215.             break;
  216.         }
  217.         break;
  218.         case 1:
  219.         /* Action is: Repeat the next element <cnt> times */
  220.         switch(s)
  221.         {
  222.             case 0:
  223.             /* Get source */
  224.             src=*(LONG *)it;
  225.             it +=sizeof(LONG);
  226.  
  227.             /* And write it. */
  228.             do
  229.             {
  230.                 *(LONG *)dst=src;
  231.                 dst+=sizeof(LONG);
  232.             }while(--cnt>=0);
  233.             break;
  234.             case 1:
  235.             src=*(WORD *)it;
  236.             it +=sizeof(WORD);
  237.             do
  238.             {
  239.                 *(WORD *)dst=src;
  240.                 dst+=sizeof(WORD);
  241.             }while(--cnt>=0);
  242.             break;
  243.             case 2:
  244.             src=*it++;
  245.             do
  246.                 *dst++=src;
  247.             while(--cnt>=0);
  248.             break;
  249.         }
  250.         break;
  251.     }
  252.  
  253.     /* Align next instruction byte */
  254.     it=(UBYTE *)(((IPTR)it+LONGALIGN-1)&~(LONGALIGN-1));
  255.     }
  256.     __AROS_FUNC_EXIT
  257. }
  258.  
  259.